package com.xzmzhp.dao.cosmetic.SQL;

import com.xzmzhp.pojo.cosmetic.Cosmetic;
import com.xzmzhp.utils.DateUtil;
import com.xzmzhp.utils.StringUtils;
import org.apache.ibatis.jdbc.SQL;

import java.math.BigDecimal;
import java.util.Date;

/**
 * @author 宋雪
 * @Date: 2020/11/19 20:50
 * @Description: 更新化妆品动态sql语句
 */

public class CosmeticSQL {
    /**
     * 更新化妆品信息
     *
     * @param cosmetic 化妆品信息
     * @return
     */
    public String updateCosmetic(Cosmetic cosmetic) {
        return new SQL() {
            {
                UPDATE("cosmetic");
                if (StringUtils.isNotEmpty(cosmetic.getCosmeticName())) {
                    SET("cosmetic_name = '" + cosmetic.getCosmeticName() + "'");
                }
                if (StringUtils.isNotNull(cosmetic.getCosmeticTypeId())) {
                    SET("cosmetic_type_id = " + cosmetic.getCosmeticTypeId());
                }
                if (StringUtils.isNotNull(cosmetic.getCosmeticPrice())) {
                    SET("cosmetic_price = " + cosmetic.getCosmeticPrice());
                }
                if (StringUtils.isNotNull(cosmetic.getCosmeticNumber())) {
                    SET("cosmetic_number = " + cosmetic.getCosmeticNumber());
                }
                if (StringUtils.isNotNull(cosmetic.getCosmeticDate())) {
                    SET("cosmetic_date = '" + DateUtil.getDateTimeToString(DateUtil.FORMAT_SHORT,cosmetic.getCosmeticDate()) + "'");
                }
                if (StringUtils.isNotNull(cosmetic.getCosmeticDate())) {
                    SET("expiry_date = '" + DateUtil.getDateTimeToString(DateUtil.FORMAT_SHORT,cosmetic.getExpiryDate()) + "'");
                }
                if (StringUtils.isNotEmpty(cosmetic.getRemark1())) {
                    SET("remark1 = '" +cosmetic.getRemark1()+ "'");
                }
                WHERE("cosmetic_id = " + cosmetic.getCosmeticId());
            }
        }.toString();
    }

    /**
     * 根据条件查询化妆品信息
     *
     * @param cosmeticTypeId 化妆品类型id
     * @param cosmeticName   化妆品名称
     * @param maxPrice       价格上限
     * @param minPrice       价格下限
     * @param remark1        化妆品状态
     * @return
     */
    public String findCosmeticByParam(Integer cosmeticTypeId, String cosmeticName, BigDecimal maxPrice, BigDecimal minPrice, String remark1) {
        return new SQL() {
            {
                SELECT("*");
                FROM("cosmetic_view");
                if (StringUtils.isNotNull(cosmeticTypeId)) {
                    WHERE("cosmetic_type_id =" + cosmeticTypeId);
                }
                if (StringUtils.isNotEmpty(cosmeticName)) {
                    WHERE("cosmetic_name like '%" +cosmeticName+ "%'");
                }
                if (StringUtils.isNotNull(maxPrice)) {
                    WHERE("cosmetic_price <=" + maxPrice);
                }
                if (StringUtils.isNotNull(minPrice)) {
                    WHERE("cosmetic_price >=" + minPrice);
                }
                if (StringUtils.isNotEmpty(remark1)) {
                    WHERE("remark1=" + remark1);
                }
            }
        }.toString();
    }
}
